﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using XbsWebAPI.Application.CheckInOut;
using XbsWebAPI.Application.CheckInOut.Dtos;
using XbsWebAPI.Application.QueryModel;
using XbsWebAPI.EntityFrameworkCore.Entities.Enums;

namespace XbsWebAPI.MainHost.Controllers
{   
    
    [ApiController]
    [Route("api/xbs_v1/[controller]")]
    public class CheckInController : ControllerBase
    {
        private readonly ICheckInAppService _CheckInAppService;

        public CheckInController(ICheckInAppService checkInAppService)
        {
            _CheckInAppService = checkInAppService;
        }


        ///<summary>
        ///条件查询/全查入住管理
        /// </summary>
        /// <param name="status">状态:0-空房，1-在住，2-预定，3-待续住，4-待退房，5-已退房</param>
        /// /// <param name="data"></param>
        /// /// <param name="pageNumber"></param>
        /// /// <param name="pageSize"></param>
        [HttpGet, Route("[action]")]
        public ResultDto<PageListDto<ListCheckInDto>> FindCheckInPagedList(RoomUsingStatus? status,string data, int? pageNumber, int? pageSize)
        {
            QueryCheckIn query = new QueryCheckIn();
            query.Data = data;
            query.RoomUsingStatus = status;
            query.PageSize = pageSize;
            query.PageNumber = pageNumber;            
            return _CheckInAppService.GetCheckInPagedList(query);
        }

        
        
        ///<summary>
        ///办理入住
        /// </summary>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> AddCheckIn(CreateCheckInDto model) {
            //验证手机号码
            Regex reg = new Regex(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
            //if (!reg.IsMatch(model.PhoneNumber)||!reg.IsMatch(model.BookingPhoneNumber))
            //{
            //    return new ResultDto<string>()
            //    {
            //        Code = -1,
            //        Message = "操作失败",
            //        Result = "手机号格式错误"
            //    };

            //}
            var result=await _CheckInAppService.AddCheckIn(model);
            return result;
        }


        ///<summary>
        ///通过id查询订单详情
        /// </summary>
        [HttpGet, Route("[action]")]
        public ResultDto<CheckInDetailDto> QueryDetail(Guid id) {
            
            return _CheckInAppService.QueryDetail(id);
        }

        /// <summary>
        /// 查询来自不同渠道的办理入住的入口
        /// </summary>
        /// <param name="orderId">点击房态管理“预定”订单点进来，必须传OrderId的值</param>
        /// <param name="roomId">点击房态管理空房间进，必须传RoomId的值</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<CheckInCreateDto> FindCheckInByBookingChannel(Guid? orderId = null,Guid? roomId = null)
        {
            //1、无orderId,无roomId，办理入住是从入住列表进入的OTA订单
            //2、无orderId，有roomId 办理入住是从房态管理进入，点击空房间的OTA订单
            //3、有orderId，无roomId 办理入住是从房态管理进入，点击已预订的小程序订单
            var result = _CheckInAppService.FindCheckInByBookingChannel(orderId, roomId);

            return result;
        }

        /// <summary>
        /// 通过营位级联查询房型
        /// </summary>
        /// <param name="campId"></param>
        /// <param name="houseTypeId"></param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<List<SelectItemDto>> FindSelectItemsFromHouseTypes(Guid? campId, Guid? houseTypeId = null)
        {
            var result = _CheckInAppService.FindSelectItemsFromHouseTypes(campId, houseTypeId);

            return result;
        }

        /// <summary>
        /// 通过房型Id级联查询房间列表和对应餐食份数和价格
        /// 1、只传CheckInDate值，查询入住当天可用房间
        /// 2、只传CheckOutDate值，查询离店当天可用房间
        /// 3、CheckInDate、CheckOutDate都传值，查询时间范围内可用的房间
        /// 4、CheckInDate、CheckOutDate都不传值查询当前房型下所有房间
        /// </summary>
        /// <param name="houseTypeId">房型Id</param>
        /// <param name="checkInDate">入住时间，传值查询当天时候有房</param>
        /// <param name="checkOutDate">离店时间，传值查询当天是否有房</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<CheckInHouseTypeDto> FindSelectItemsFromRoomsAndOther(Guid? houseTypeId,DateTime? checkInDate,DateTime? checkOutDate)
        {
            var result = _CheckInAppService.FindSelectItemsFromRoomsAndOther(houseTypeId,checkInDate,checkOutDate);

            return result;
        }

    }
}
